package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.HosPeoEntity;
import com.css.fxfzypg.modules.baseData.entity.VillageTownHosPeoEntity;
import com.css.fxfzypg.modules.baseData.queryParams.VillageTownHosPeoQueryParams;
import com.css.fxfzypg.modules.baseData.service.VillageTownHosPeoService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author dbj
 * @create 2022-11-14 11:33
 * 乡镇人口和建筑物概况子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData/villageTown")
@Slf4j
public class VillageTownHosPeoController {

    @Resource
    private VillageTownHosPeoService hosPeoService;

    @PostMapping("/saveVillageTownHosPeo")
    public RestResponse saveHosPeo(@RequestBody VillageTownHosPeoEntity hosPeoEntity){
        return hosPeoService.saveHosPeo(hosPeoEntity);
    }

    @GetMapping("/getVillageTownHosPeoById/{id}")
    public RestResponse getHosPeoById(@PathVariable("id")String id){
        return ResponseCreator.createSuccessResponse(hosPeoService.fingHosPeoById(id));
    }

    @PostMapping("/updateVillageTownHosPeo")
    public RestResponse updateHosPeo(@RequestBody VillageTownHosPeoEntity hosPeoEntity){
        return hosPeoService.updateHosPeo(hosPeoEntity);
    }

    @GetMapping("/getAllVillageTownHosPeo")
    public RestResponse getAllHisPr(HttpServletRequest request, VillageTownHosPeoQueryParams queryParams){
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = hosPeoService.getAllHosPeoByParams(queryParams, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            log.error("查询人口和建筑物概况失败!", e);
            restResponse = RestResponse.fail("查询人口和建筑物概况失败");
        }
        return restResponse;
    }

    @PostMapping("/deleteVillageTownHosPeo/{id}")
    public RestResponse deleteHosPeo(@PathVariable("id") String id){
        try {
            hosPeoService .deleteHosPeo(id);
            return ResponseCreator.createSuccessResponse(null,"删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }
    @PostMapping("/hosPeoExportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        try {
            // hosPeoService.exportExcel(response);
        log.error("功能暂时缺失");
        } catch (Exception e) {
            log.error("导出失败!",e);
        //     RestResponse.fail("导出失败!");
        }
    }

    @PostMapping("/hosPeoImportExcel")
    public RestResponse importExcel(MultipartFile file)throws IOException {

        try {
            return hosPeoService.hosPeoImportExcel(file);
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestResponse.fail("导入失败，" + e.getMessage());
        }
    }

    @PostMapping("/cover")
    public RestResponse cover(@RequestBody List<VillageTownHosPeoEntity> list){
        RestResponse restResponse = null;
        try {
            hosPeoService.cover(list);
            restResponse=RestResponse.succeed("成功导入"+list.size()+"条.");
        } catch (Exception e) {
            log.error("覆盖人口和建筑物概况失败",e);
            restResponse =   RestResponse.fail("覆盖人口和建筑物概况失败");
        }
        return restResponse;
    }

}
